package com.gitee.dbswitch.provider.sync;

import com.gitee.dbswitch.common.util.ObjectCastUtils;
import com.gitee.dbswitch.provider.ProductFactoryProvider;

import java.util.ArrayList;
import java.util.List;

public class AutoCastTableDataSynchronizeProvider extends DefaultTableDataSynchronizeProvider {

    public AutoCastTableDataSynchronizeProvider(ProductFactoryProvider factoryProvider) {
        super(factoryProvider);
    }

    @Override
    public long executeInsert(List<Object[]> records) {
        List<Object[]> recordstemp = new ArrayList<>(records.size());
        for (Object[] row : records) {
            Object[] rowTemp = new Object[row.length];
            for (int i = 0; i < row.length; ++i) {
                // 对于 User 对象，假设 User 有一个复制构造函数
                rowTemp[i] = ObjectCastUtils.castByJdbcType(insertArgsType[i], row[i]);
            }
            recordstemp.add(rowTemp);
        }
        return super.executeInsert(recordstemp);
    }

    @Override
    public long executeUpdate(List<Object[]> records) {
        List<Object[]> recordstemp = new ArrayList<>(records.size());
        for (Object[] row : records) {
            Object[] rowTemp = new Object[row.length];
            for (int i = 0; i < row.length; ++i) {
                // 对于 User 对象，假设 User 有一个复制构造函数
                rowTemp[i] = ObjectCastUtils.castByDetermine(row[i]);
            }
            recordstemp.add(rowTemp);
        }
        return super.executeUpdate(recordstemp);
    }
}
